package com.hst.capacity.facade.rest;

import com.hst.capacity.application.biz.AccountBiz;
import com.hst.capacity.application.biz.AccountDriverBiz;
import com.hst.capacity.application.biz.TokenBiz;
import com.hst.capacity.application.export.XssfExporter;
import com.hst.capacity.application.export.XssfImporter;
import com.hst.capacity.domain.common.constants.Constants;
import com.hst.capacity.domain.exception.BusinessException;
import com.hst.capacity.domain.model.request.account.*;
import com.hst.capacity.domain.model.request.finance.OrderStatementParam;
import com.hst.capacity.domain.model.response.DefaultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import static com.hst.capacity.domain.model.response.DefaultVO.success;

/**
 *  分账模块Controller
 *
 * */
@RequestMapping()
@RestController
public class AccountController {

    @Autowired
    private AccountBiz accountBiz;

    @Autowired
    private AccountDriverBiz accountDriverBiz;

    @Autowired
    TokenBiz tokenBiz;


//    /**
//     * 城际收支账户
//     * @param param
//     * @return
//     */
//    @PostMapping("/account/select")
//    private DefaultVO accountSelect(@RequestBody @Valid AccountParam param){
//        return success(accountBiz.search(param));
//    }

    /**
     * 城际收支账户
     * @param param
     * @return
     */
    @PostMapping("/intercity/account/select")
    private DefaultVO intercityAccountSelect(@RequestBody @Valid IntercityAccountParam param){
        return success(accountBiz.searchIntercity(param));
    }

    /**
     * 城际收支账单
     * @param param
     * @return
     */
    @PostMapping("/intercity/bill/search")
    private DefaultVO intercityBillSearch(@RequestBody @Valid IntercityBillParam param){
        return success(accountBiz.intercityBillSearch(param));
    }

    /**
     * 快车收支账户
     * @param param
     * @return
     */
    @PostMapping("/express/account/select")
    private DefaultVO expressAccountSelect(@RequestBody @Valid ExpressAccountParam param){
        return success(accountBiz.expressAccountSelect(param));
    }

    /**
     * 快车收支账单
     * @param param
     * @return
     */
    @PostMapping("/express/bill/search")
    private DefaultVO expressBillSearch(@RequestBody @Valid ExpressBillParam param){
        return success(accountBiz.expressBillSearch(param));
    }

    /**
     * 专车收支账户
     * @param param
     * @return
     */
    @PostMapping("/special/account/select")
    private DefaultVO specialAccountSelect(@RequestBody @Valid SpecialAccountParam param){
        return success(accountBiz.specialAccountSelect(param));
    }

    /**
     * 专车收支账单
     * @param param
     * @return
     */
    @PostMapping("/special/bill/search")
    private DefaultVO specialBillSearch(@RequestBody @Valid SpecialBillParam param){
        return success(accountBiz.specialBillSearch(param));
    }

    /**
     * 运营商提现账号信息
     * @param param
     * @return
     */
    @PostMapping("/franchisee/deposit/select")
    private DefaultVO franchiseeDepositSelect(@RequestBody @Valid FranchiseeDepositSelectParam param){
        return success(accountBiz.franchiseeDepositSelect(param));
    }

    /**
     * 运营商提现明细
     * @param param
     * @return
     */
    @PostMapping("/franchisee/deposit/detail/search")
    private DefaultVO franchiseeDepositDetailSearch(@RequestBody @Valid FranchiseeDepositDetailParam param){
        return success(accountBiz.franchiseeDepositDetailSearch(param));
    }

    /**
     * 城际收支账户
     * @param param
     * @return
     */
    @PostMapping("/account/driver/detail")
    private DefaultVO searchDriverAccountDetail(@RequestBody @Valid AccountDriverParam param) {
        return success(accountDriverBiz.searchDriverAccountDetail(param));
    }

    /**
     * 城际收支账户
     * @param param
     * @return
     */
    @PostMapping("/account/driver")
    private DefaultVO searchDriverAccount(@RequestBody AccountSearchParam param) {
        return success(accountDriverBiz.searchDriverAccountList(param));
    }

    /**
     * 城际收支账户
     * @param param
     * @return
     */
    @PostMapping("/order/statement/sreach")
    private DefaultVO orderStatementSearch(@RequestBody OrderStatementParam param, HttpServletRequest request) {
        param.setIdUser(tokenBiz.findIdUser(request));
        return success(accountBiz.orderStatementSearch(param));
    }

    /**
     * 对账数据上传
     * @return
     */
    @PostMapping("/account/upload")
    private DefaultVO uploadAccountData(@RequestParam("type")int type, @RequestParam("file") MultipartFile multipartFile) {

        if (multipartFile.isEmpty()) {
            throw new BusinessException(4001,"参数异常，文件内容异常。");
        }
        // 当文件超过设置的大小时2M暂定，则不运行上传
        if (multipartFile.getSize() > Constants.FILE_SIZE) {
            throw new BusinessException(4001,"文件异常，文件过大。");
        }

        accountBiz.doMatchAccount(multipartFile, type);

        //xssfImporter.importAccountData(multipartFile, Constants.OrderSource.ALITRIP);

        return success(null);
    }

}
